﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : tian_
// Created          : 09-06-2018
//
// Last Modified By : tian_
// Last Modified On : 09-07-2018
// ***********************************************************************
// <copyright file="ProjectSiteFormationCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.IO;
using Osg;
using OSS.BIM.Model;
using OSS.BIM.SceneManage;
using OSS.BIM.Utility;
using VectorDraw.Professional.vdFigures;
using ZFGK.Addins;
using ZFGK.GIS.Utility;
using ZFGK.OSG.Utility;
using ZFGK.osgEarth.UI;
using ZFGK.osgEarth.Utility;
using ZFGK.osgEarth.Web;
using ZFGK.OsgVdf.Utility;
using ZFGK.Utility;
using ZFGK.VDF.Entity;

namespace OSS.BIM.Commands.ProjcetCmds
{
    /// <summary>
    /// Class ProjectSiteFormationCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class ProjectSiteFormationCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var osgEarthViewForm = cmdData.ViewForm as OsgEarthViewForm;
            if (osgEarthViewForm == null)
                return CmdResult.Failure;
            var osgEarthViewCtrl = osgEarthViewForm.OsgEarthViewCtrl;
            var osgEarthObj = osgEarthViewCtrl.OsgEarthObj;
            var actionUtility = osgEarthViewCtrl.ActionUtility;

            var project = cmdData.Tag as Project;
            if (project == null)
                return CmdResult.Cancel;

            if (string.IsNullOrEmpty(project.BorderPoints))
            {
                ZfMessageUtil.ShowError("不存在边界点！");
                return CmdResult.Cancel;
            }

            var location = project.GetLocation();
            var rMat = Matrixd.rotate(project.Rotation, new Vec3d(){z = 1});
            var toWolrdMat = location.CreateLocalToWorld(osgEarthObj.MapNode.getMapSRS());

            var verts = project.BorderPoints.ParseVec3ds();
            rMat.Transform(verts);

            var box = verts.GetBoundingBox();

            // 根据经纬度范围下载Dem(稍微外扩一点儿)
            double width = box.xMax - box.xMin;
            double height = box.yMax - box.yMin;
            Vec3d surfDemCenterPt = new Vec3d((box.xMin + box.xMax) * 0.5, (box.yMin + box.yMax) * 0.5, 0);
            Vec3d surfDemCenterMapPt = (surfDemCenterPt * toWolrdMat).ToMap(osgEarthObj.MapNode.getMapSRS());
            var gridDem = OeWebDataUtil.DownloadDem(osgEarthObj.MapNode.getMapSRS(), surfDemCenterMapPt, width, height, 10, 14);

            // 对机场范围内的区域进行修改标高
            var polyline = new vdPolyline(null, verts.TogPoints());
            var matInv = toWolrdMat.GetInvertion();
            for (int i = 0; i < gridDem.RowCount; i++)
            {
                for (int j = 0; j < gridDem.ColCount; j++)
                {
                    double x = gridDem.XMin + j * gridDem.Spacing;
                    double y = gridDem.YMin + i * gridDem.Spacing;
                    double rawElev = gridDem.GetElevation(x, y);
                    var mapPt = new Vec3d(x, y, rawElev);
                    var pt = mapPt.ToWorld(osgEarthObj.MapNode.getMapSRS()) * matInv;

                    if (vdPolylineUtil.IsIn(polyline, pt.TogPoint()))
                    {
                        gridDem.SetElevation(i, j, project.Elevation);
                    }
                }
            }

            string tiffFilePath = project.GetCutFillTifFilePath();
            GdalUtil.SaveTif(gridDem, tiffFilePath, 14);

            string tiffFileTempPath = project.GetCutFillTifFileTempPath();
            File.Copy(tiffFilePath, tiffFileTempPath);

            if (BmModelManager.ElevationLayerMap.ContainsKey(project.ProjectId))
            {
                var elevationLayerRe = BmModelManager.ElevationLayerMap[project.ProjectId];
                osgEarthObj.RemoveLayer(elevationLayerRe);
                BmModelManager.ElevationLayerMap.Remove(project.ProjectId);
            }
            var elevationLayer = OeLayerUtil.NewElevationLayer(tiffFileTempPath, project.ProjectId.ToString());
            osgEarthObj.AddLayer(elevationLayer);
            BmModelManager.ElevationLayerMap.Add(project.ProjectId, elevationLayer);

            return CmdResult.Failure;
        }
    }
}
